package com.simple.ds.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.google.common.primitives.Longs;
import com.simple.common.entity.po.TradeEntity;
import com.simple.common.entity.po.sys.RefundTrade;
import com.simple.ds.service.RefundTradeApi;
import com.simple.ds.service.RefundTradeService;
import com.simple.ds.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author yasud
 * @date 2021年09月20日 14:11
 * @description
 */
@Service
public class RefundTradeApiImpl implements RefundTradeApi {

    @Autowired
    RefundTradeService refundTradeService;

    @Autowired
    TradeService tradeService;

    @Override
    public RefundTrade findByTradeId(Long value) {
        TradeEntity tradeEntity = tradeService.getById(value);
        return createRefundTrade(tradeEntity);
    }

    @Override
    public RefundTrade save_(RefundTrade refundTrade) {
        return refundTrade;
    }

    private RefundTrade createRefundTrade(TradeEntity tradeEntity) {
        if (Objects.isNull(tradeEntity)) return null;
        RefundTrade refundTrade = new RefundTrade();
        refundTrade.setId(tradeEntity.getId());
        refundTrade.setApplicationId(Longs.fromByteArray(DigestUtil.sha256(tradeEntity.getMerchantCode() + "_application")));
        refundTrade.setAppId(tradeEntity.getMerchantCode());
        refundTrade.setAppName(tradeEntity.getMerchantName());
        refundTrade.setMerchantNo(tradeEntity.getMerchantCode());
        refundTrade.setMerchantName(tradeEntity.getMerchantName());
        refundTrade.setChannelCode(tradeEntity.getChannelMerchantName());
        refundTrade.setChannelName(tradeEntity.getChannelMerchantName());
        refundTrade.setTradeId(tradeEntity.getId());
        refundTrade.setTradeAmount(tradeEntity.getOrderAmount() + "");
        refundTrade.setRealRefundTotalAmount(tradeEntity.getRefundAmount() + "");
        refundTrade.setCurrency(tradeEntity.getRealCurrency());
        refundTrade.setRealRefundCurrency(tradeEntity.getRealCurrency());
        refundTrade.setUpdateTime(tradeEntity.getUpdateTime());
        refundTrade.setCreateTime(tradeEntity.getCreateTime());
        return refundTrade;
    }

    @Override
    public RefundTrade updateById_(RefundTrade refundTrade) {
        return refundTrade;
    }
}
